package com.wmcom.wm.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wmcom.wm.common.R;
import com.wmcom.wm.entity.Category;
import com.wmcom.wm.entity.Dish;
import com.wmcom.wm.entity.Setmeal;
import com.wmcom.wm.service.CategoryService;
import com.wmcom.wm.service.DishService;
import com.wmcom.wm.service.SetmealService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 分类管理
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Resource
    private CategoryService categoryService;
    @Resource
    private DishService dishService;
     @Resource
     private SetmealService setmealService;
    /**
     * 新增菜品分类
     * @param category
     * @return
     */
 @PostMapping
 public R<String>category(@RequestBody Category category){
     System.out.println(category);
        if (category==null) {
               return R.error("添加异常！");
        }
     categoryService.save(category);

     return R.success("添加成功！");
 }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
   @GetMapping("/page")
    public R<Page> page(int page,int pageSize) {
          Page<Category> pa = new Page<>(page,pageSize);
          LambdaQueryWrapper<Category>wrap=new LambdaQueryWrapper<>();
          wrap.orderByAsc(Category ::getSort);
          categoryService.page(pa,wrap);
         return R.success(pa);
    }

    /**
     * 根据id删除分类
     * @param
     * @return
     */
   @DeleteMapping
    public R<String> delete( Long ids) {
       Category category = categoryService.getById(ids);
        if (category.getType()==2){
            List<Setmeal> setmeal = setmealService.list(new LambdaQueryWrapper<Setmeal>().eq(Setmeal::getCategoryId, ids));
             if (setmeal.size()> 0) {
                 return R.error("删除失败,套餐还有绑定套餐");
             }
            setmealService.removeById(ids);
        }else if (category.getType() == 1){
            List<Dish> dishList = dishService.list(new LambdaQueryWrapper<Dish>().eq(Dish::getCategoryId, ids));
            if (dishList.size() > 0){
                return R.error("删除失败,菜品还有绑定菜品");
            }
            dishService.removeById(ids);
        }
       return R.error("删除成功！");
   }

    /**
     * 根据id修改分类信息
     * @param category
     * @return
     */
@PutMapping
     public R<String> updateCategory(@RequestBody Category category){
          if (category == null) {
               return R.error("修改异常");
          }
          categoryService.updateById(category);
          return R.success("修改成功！");
}
    /**
     *
     * 根据条件查询分类数据
     */
    @GetMapping("/list")
    public R<List<Category>> list(Category category) {
         LambdaQueryWrapper<Category>wrapper = new LambdaQueryWrapper<>();
         if (category.getType()!=null){
               wrapper.eq(Category ::getType,category.getType());
         }
        List<Category> list = categoryService.list(wrapper);
        return R.success(list);
    }

}
